package com.ruoyi.jwxt.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.RepairStatusConstants;
import com.ruoyi.jwxt.domain.DTO.JwxtLabMaintenanceDTO;
import com.ruoyi.jwxt.domain.PO.JwxtLabMaintenance;
import com.ruoyi.jwxt.mapper.JwxtLabMaintenanceMapper;
import com.ruoyi.jwxt.service.IJwxtLabMaintenanceService;

/**
 * 设备报修Service业务层处理
 */
@Service
public class JwxtLabMaintenanceServiceImpl implements IJwxtLabMaintenanceService {
    @Autowired
    private JwxtLabMaintenanceMapper jwxtLabMaintenanceMapper;
    @Autowired
    private JwxtLabsServiceImpl jwxtLabsService;

    /**
     * JwxtLabMaintenance转换为JwxtLabMaintenanceDTO
     */
    @Override
    public JwxtLabMaintenanceDTO convert(JwxtLabMaintenance jwxtLabMaintenance) {
        JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO = new JwxtLabMaintenanceDTO();
        jwxtLabMaintenanceDTO.setRepairId(jwxtLabMaintenance.getRepairId());
        jwxtLabMaintenanceDTO.setTeacherId(jwxtLabMaintenance.getTeacherId());
        jwxtLabMaintenanceDTO.setLabId(jwxtLabMaintenance.getLabId());
        jwxtLabMaintenanceDTO.setFaultDescription(jwxtLabMaintenance.getFaultDescription());
        jwxtLabMaintenanceDTO.setRepairDate(jwxtLabMaintenance.getRepairDate());
        jwxtLabMaintenanceDTO.setExperimenterId(jwxtLabMaintenance.getExperimenterId());
        jwxtLabMaintenanceDTO.setRepairStatus(jwxtLabMaintenance.getRepairStatus());
        jwxtLabMaintenanceDTO.setRepairDescription(jwxtLabMaintenance.getRepairDescription());
        
        return jwxtLabMaintenanceDTO;
    }

    /**
     * JwxtLabMaintenanceDTO转换为JwxtLabMaintenance
     */
    public JwxtLabMaintenance convert(JwxtLabMaintenanceDTO jwxtLabMaintenanceDTO) {
        JwxtLabMaintenance jwxtLabMaintenance = new JwxtLabMaintenance();
        jwxtLabMaintenance.setRepairId(jwxtLabMaintenanceDTO.getRepairId());
        jwxtLabMaintenance.setTeacherId(jwxtLabMaintenanceDTO.getTeacherId());
        jwxtLabMaintenance.setLabId(jwxtLabMaintenanceDTO.getLabId());
        jwxtLabMaintenance.setFaultDescription(jwxtLabMaintenanceDTO.getFaultDescription());
        jwxtLabMaintenance.setRepairDate(jwxtLabMaintenanceDTO.getRepairDate());
        jwxtLabMaintenance.setExperimenterId(jwxtLabMaintenanceDTO.getExperimenterId());
        jwxtLabMaintenance.setRepairStatus(jwxtLabMaintenanceDTO.getRepairStatus());
        jwxtLabMaintenance.setRepairDescription(jwxtLabMaintenanceDTO.getRepairDescription());
        return jwxtLabMaintenance;
    }

    /**
     * 新增设备报修
     */
    @Override
    public int insert(JwxtLabMaintenance jwxtLabMaintenance) {
        jwxtLabMaintenance.setRepairDate(new Date());

        /*根据实验室id获取到负责实验员id*/
        Long labId = jwxtLabMaintenance.getLabId();
        Long expId = jwxtLabsService.select(labId).getExperimenterId();
        jwxtLabMaintenance.setExperimenterId(expId);

        return jwxtLabMaintenanceMapper.insert(jwxtLabMaintenance);
    }

    /**
     * 批量删除设备报修
     */
    @Override
    public int delete(Long[] repairIds) {
        return jwxtLabMaintenanceMapper.delete(repairIds);
    }

    /**
     * 删除设备报修
     */
    @Override
    public int delete(Long repairId) {
        return jwxtLabMaintenanceMapper.delete(new Long[] { repairId });
    }

    /**
     * 查询设备报修列表
     */
    @Override
    public List<JwxtLabMaintenance> select(JwxtLabMaintenance jwxtLabMaintenance) {
        return jwxtLabMaintenanceMapper.select(jwxtLabMaintenance);
    }

    /**
     * 查询设备报修
     */
    @Override
    public JwxtLabMaintenance select(Long repairId) {
        JwxtLabMaintenance jwxtLabMaintenance = new JwxtLabMaintenance();
        jwxtLabMaintenance.setRepairId(repairId);
        List<JwxtLabMaintenance> jwxtLabMaintenances = select(jwxtLabMaintenance);
        if(jwxtLabMaintenances.isEmpty()){
            return null;
        }
        return jwxtLabMaintenances.get(0);
    }

    /**
     * 查询全部设备报修列表
     */
    @Override
    public List<JwxtLabMaintenance> select() {
        return select(new JwxtLabMaintenance());
    }

    /**
     * 修改设备报修
     */
    @Override
    public int update(JwxtLabMaintenance jwxtLabMaintenance) {
        /*根据实验室id获取到负责实验员id*/
        Long labId = jwxtLabMaintenance.getLabId();
        Long expId = jwxtLabsService.select(labId).getExperimenterId();
        jwxtLabMaintenance.setExperimenterId(expId);

        return jwxtLabMaintenanceMapper.update(jwxtLabMaintenance);
    }

    /**
     * 开始维修时，把报修申请的状态设置为“维修中”
     */
    @Override
    public int start(JwxtLabMaintenance jwxtLabMaintenance) {
        if (jwxtLabMaintenanceMapper.select(jwxtLabMaintenance).isEmpty()) {
            return 0;
        }
        if (jwxtLabMaintenance.getRepairStatus().equals(RepairStatusConstants.NOT_REPAIRED)) {
            jwxtLabMaintenance.setRepairStatus(RepairStatusConstants.REPAIRING);
            return jwxtLabMaintenanceMapper.update(jwxtLabMaintenance);
        }
        return 0;
    }

    /**
     * 维修结束后，把报修申请的状态设置为“已维修”，并填写维修情况说明
     */
    @Override
    public int end(JwxtLabMaintenance jwxtLabMaintenance) {
        if (jwxtLabMaintenanceMapper.select(jwxtLabMaintenance).isEmpty()) {
            return 0;
        }
        if (jwxtLabMaintenance.getRepairStatus().equals(RepairStatusConstants.REPAIRING)) {
            jwxtLabMaintenance.setRepairStatus(RepairStatusConstants.REPAIRED);
            return jwxtLabMaintenanceMapper.update(jwxtLabMaintenance);
        }
        return 0;
    }

}
